(function (exports) {

    var $ = null;

    //001 004 008 009 012 013
    var heroArr =  [
        '013',
        '001',
        '004',
        '008',
        '009',
        '012',
    ];
    //atk  atk0 atk1 atk2 atkd death idle idle2 run vertigo win win2
    var attArr = [
        'atk',
        'atk0',
        'atk1',
        'atk2',
    ];
    var ChooseRoleLayer = cc.Layer.extend({

        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
        },
        onCCSLoadFinish: function () {
            var that = this;
        },
        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
        },
        ctor: function () {
            this._super();

            var that = this;

            loadCCSTo(res.ChooseRoleLayer_json, this, "Layer");
            $ = create$(this.getChildByName("Layer"));
            shipei($);

            TouchUtils.setOnclickListener($('top.close'), function (node) {
                that.removeFromParent();
            });
            for(var i=0;i<heroArr.length;i++){
                $('root.choosebg.avatar_' + i).setTexture('res/image/avatar/avatar_' + parseInt(heroArr[i]) + '.jpg');
            }

            this.animIndex = 0;

            this.initUI();
            this.initHero(1);
            this.initNickName();
            TouchUtils.setOnclickListener($('root.herobg'), function (node) {
                that.animIndex++;
                that.animIndex = that.animIndex % attArr.length;
                if(that.hero)  that.hero.addAnimation(0, attArr[that.animIndex], true);
            });

        },
        initHero: function(heroIndex){
            if(!cc.sys.isNative)  return;

            if(heroIndex != this.heroIndex){
                console.log(heroIndex);
                console.log(this.heroIndex);
                if(this.hero)  this.hero.removeFromParent();

                var hero = new sp.SkeletonAnimation('res/image/spine/' + heroArr[heroIndex] + '.json',
                    'res/image/spine/' + heroArr[heroIndex] + '.atlas');
                hero.setPosition(cc.p(100, 0));
                $('root.herobg').addChild(hero);
                hero.addAnimation(0, attArr[this.animIndex], true);
                this.hero = hero;

                this.heroIndex = heroIndex;
            }
        },
        initUI: function(){
            var that = this;
            var touchLayer = new cc.LayerColor(cc.color(0, 0, 0, 5), 300, cc.winSize.height);
            touchLayer.setName("touchLayer");
            touchLayer.setAnchorPoint(0, 0);
            this.addChild(touchLayer);

            var touchpos = null;
            that.moving = false;
            var chupaiListener = cc.EventListener.create({
                event: cc.EventListener.TOUCH_ONE_BY_ONE,
                swallowTouches: false,
                onTouchBegan: function (touch, event) {
                    if(TouchUtils.isTouchMe(touchLayer, touch, event, null)) {
                        if (that.moving)  return;
                        touchpos = touch.getLocation();
                        that.moving = true;
                        return true;
                    }
                    return false;
                },
                onTouchMoved: function (touch, event) {
                    var moveposy = touch.getLocation().y - touchpos.y;
                    touchpos = touch.getLocation();

                    $('root.choosebg').setRotation($('root.choosebg').getRotation() - moveposy);
                },
                onTouchEnded: function (touch, event) {
                    touchpos = null;

                    var currotate = Math.abs($('root.choosebg').getRotation() % 360 + 360) % 360;
                    var minrotate = 90;
                    var curindex = 1;
                    var moverotate = 0;
                    for(var i=0;i<heroArr.length;i++){
                        var curhero = (i*60 + 30 + currotate) % 360;
                        if(Math.abs(curhero - 90) < minrotate){
                            minrotate = Math.abs(curhero - 90);
                            moverotate = 90 - curhero;
                            curindex = i;
                        }
                    }
                    $('root.choosebg').runAction(cc.sequence(
                        cc.rotateBy(0.1, moverotate),
                        cc.callFunc(function(){
                            that.moving = false;
                            that.initHero(curindex);
                        })
                    ))
                }
            });
            return cc.eventManager.addListener(chupaiListener, touchLayer);
        },
        initNickName: function(){
            var normal9SpriteBg = new cc.Scale9Sprite('res/image/common/chatroom_input_back.png');
            var press9SpriteBg = new cc.Scale9Sprite('res/image/common/chatroom_input_back.png');
            var disabled9SpriteBg = new cc.Scale9Sprite('res/image/common/chatroom_input_back.png');
            var editbox = new cc.EditBox(cc.size(320, 50),normal9SpriteBg,press9SpriteBg,disabled9SpriteBg);
            editbox.setPosition(cc.p(420, 40));
            editbox.setFontSize(30);
            editbox.setPlaceHolder("输入昵称");
            editbox.setMaxLength(15);
            editbox.setFontColor(cc.color(255, 255, 255, 100));
            $('root').addChild(editbox);
        },
    });
    exports.ChooseRoleLayer = ChooseRoleLayer;
})(window);
